package com.baosight.risk.service.code.impl;

import com.baosight.risk.api.code.ISqlCodeService;
import com.baosight.risk.base.redis.BaseRedis;
import com.baosight.risk.mapper.code.SqlCodeMapper;
import com.baosight.risk.service.code.entity.SqlCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 数据库异常编码
 */
@Service
public class SqlCodeServiceImpl implements ISqlCodeService {

    @Autowired
    private BaseRedis redis;

    @Autowired
    private SqlCodeMapper sqlCodeMapper;

    /**
     * 换成code key
     */
    private static final String SQL_CODE_KEY = "redis:sql:code";


    /**
     * 查询全部code放入redis
     *
     * @return
     */
    @Override
    public List<SqlCode> queryAll() {
        if (!redis.isKey(SQL_CODE_KEY)) {
            redis.setValue(SQL_CODE_KEY, sqlCodeMapper.selectAll());
        }
        return (List<SqlCode>) redis.getValue(SQL_CODE_KEY);
    }


    @Override
    public List<SqlCode> select(SqlCode t) {
        return sqlCodeMapper.select(t);
    }

    /**
     * 清空redis换成
     */
    @Override
    public void clearSqlCode() {
        redis.delete(SQL_CODE_KEY);
    }

    @Override
    public void delete(SqlCode sql) {
        sqlCodeMapper.insert(sql);
    }
}
